package cn.openea.perpartner.componet.web.controller;


import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.common.core.utils.RegexUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.componet.web.entity.SmsHistory;
import cn.openea.perpartner.componet.web.entity.SmsSign;
import cn.openea.perpartner.componet.web.entity.SmsTemplate;
import cn.openea.perpartner.componet.web.entity.dto.SmsHistoryDTOFactory;
import cn.openea.perpartner.componet.web.entity.dto.sms.SendDetail;
import cn.openea.perpartner.componet.web.entity.dto.sms.SmsDto;
import cn.openea.perpartner.componet.web.entity.vo.SmsTemplateVo;
import cn.openea.perpartner.componet.web.enums.ScopeEnums;
import cn.openea.perpartner.componet.web.service.ISmsService;
import com.aliyuncs.exceptions.ClientException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


@Api(tags = "短信管理")
@RestController
@RequestMapping("/sms")
public class SmsController extends BaseController {

    @Autowired
    private ISmsService smsService;
    @Autowired
    private SmsHistoryDTOFactory historyDTOFactory;

    @ApiOperation("发送短信")
    @PostMapping("/send")
    public AjaxResult sendMsg(@RequestBody @Valid SmsDto smsDto) {
        if (!RegexUtil.matchCellphone(smsDto.getPhoneNumber())) {
            return AjaxResult.error("手机号码格式不正确");
        }
        return toAjax(smsService.sendMsg(smsDto));
    }
    @ApiOperation("查询短信发送详情")
    @PostMapping("/sendDetails")
    public AjaxResult QuerySendDetails(@RequestBody SendDetail details)  {

        return AjaxResult.success(smsService.QuerySendDetails(details));
    }
    @ApiOperation("批量发送短信")
    @PostMapping("/batch/send")
    public AjaxResult batchSendMsg(@RequestBody SmsDto smsDto) throws ClientException {

        return toAjax(smsService.batchSendMsg(smsDto));
    }
    @ApiOperation("发送范围")
    @GetMapping(value = "/scope")
    public TableDataInfo selectSendScope() {

        return getDataTable(ScopeEnums.getScopeEnums());
    }

    @ApiOperation("发送记录")
    @PostMapping(value = "/history/list")
    public TableDataInfo selectHistoryList(@RequestBody SmsHistory sms) {
        startPage();
        List<SmsHistory> list = this.smsService.selectHistoryList(sms);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setRows(list.stream()
                .map(item -> historyDTOFactory.toHistoryDTO.apply(item))
                .collect(Collectors.toList()));
        return dataTable;
    }
    @ApiOperation("保存模板")
    @PostMapping("/template/save")
    public AjaxResult saveSmsTemplate(@RequestBody @Valid SmsTemplate smsTemplate) {
        return toAjax(smsService.saveSmsTemplate(smsTemplate));
    }
    @ApiOperation("删除模板")
    @PostMapping("/template/delete")
    public AjaxResult deleteSmsTemplate(@RequestBody List<Long> idList) {

        return toAjax(smsService.deleteSmsTemplate(idList));
    }

    @ApiOperation("模板列表")
    @GetMapping("/template/list")
    public AjaxResult selectTemplateList() {
        List<SmsTemplate> list = smsService.selectTemplateList(null);
        return AjaxResult.success(list);
    }

    @ApiOperation("分页查询")
    @PostMapping("/template/page/list")
    public TableDataInfo templatePageList(@RequestBody SmsTemplateVo templateVo) {
        startPage();
        List<SmsTemplate> list = smsService.selectTemplateList(templateVo);
        return getDataTable(list);
    }
    @ApiOperation("模板详情")
    @GetMapping("/findTemplate/{id}")
    public AjaxResult findTemplateById(@PathVariable String id) {
        SmsTemplate template = Optional.ofNullable(smsService.selectTemplateById(id))
                .orElseThrow(() -> new ServiceException("模板不存在"));
        return AjaxResult.success(template);
    }
    @ApiOperation("模板详情")
    @GetMapping("/{code}/findTemplate/")
    public AjaxResult findTemplateByCode(@PathVariable String code) {
        SmsTemplate template = Optional.ofNullable(smsService.selectTemplateByCode(code))
                .orElseThrow(() -> new ServiceException("模板不存在"));
        return AjaxResult.success(template);
    }

    @ApiOperation("签名列表分页")
    @GetMapping(value = "/sign/page/list")
    public TableDataInfo selectSignPageList(SmsSign sign) {
        startPage();
        List<SmsSign> list = smsService.selectSignList(sign);
        return getDataTable(list);
    }

    @ApiOperation("签名列表")
    @GetMapping(value = "/sign/list")
    public AjaxResult selectSignList(SmsSign sign) {
        List<SmsSign> list = smsService.selectSignList(sign);
        return AjaxResult.success(list);
    }


    @ApiOperation("保存签名")
    @PostMapping("/sign/save")
    public AjaxResult saveSmsSign(@RequestBody @Valid SmsSign sign) {
        smsService.saveSmsSign(sign);
        return AjaxResult.success();
    }

    @ApiOperation("查询签名")
    @GetMapping("/sign/{id}")
    public AjaxResult selectSignById(@PathVariable String id) {
        return success(smsService.selectSignById(id));
    }

    @ApiOperation("删除签名")
    @PostMapping("/sign/delete")
    public AjaxResult deleteSmsSign(@RequestBody List<Long> idList) {
        return toAjax(smsService.deleteSmsSign(idList));
    }
}

